package com.cmict.data.system.controller.admin.common;


import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.cmict.data.framework.common.enums.ConfKeyEnum;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.system.controller.admin.conf.vo.ConfSystem;
import com.cmict.data.system.dal.redis.auth.LoginLockRedisDAO;
import com.cmict.data.system.service.conf.ConfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.cmict.data.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cmict.data.framework.common.pojo.CommonResult.error;
import static com.cmict.data.framework.common.pojo.CommonResult.success;
import static com.cmict.data.framework.common.util.servlet.ServletUtils.getClientIP;
import static com.cmict.data.system.enums.ErrorCodeConstants.AUTH_CAPTCHA_BAD;

@Api(tags = "管理后台 - 验证码")
@RestController
@RequestMapping("/system/captcha")
public class Captcha2Controller {

    @Autowired
    private CaptchaService captchaService;

    @Autowired
    private ConfService confService;

    @Resource
    private LoginLockRedisDAO loginLockRedisDAO;
    @GetMapping("/get")
    @ApiOperation("生成滑块图片验证码")
    public CommonResult<ResponseModel> get() {
//        String clientIP = getClientIP();
//        String captchaTenantId = "0";
//        String username = "captcha";
//        //登录延迟判断
//        if(StringUtils.isEmpty(loginLockRedisDAO.get(captchaTenantId,username ,clientIP))){
//            loginLockRedisDAO.set(captchaTenantId,username ,clientIP,1);
//        }else{
//            int num = Integer.valueOf(loginLockRedisDAO.get(captchaTenantId,username ,clientIP));
//            num= num +1;
//            if(num >= 5 ){
//                loginLockRedisDAO.set(captchaTenantId,username ,clientIP,num);
//                throw exception(AUTH_CAPTCHA_BAD);
//            }
//            loginLockRedisDAO.set(captchaTenantId,username ,clientIP,num);
//        }

        CaptchaVO vo = new CaptchaVO();
//        vo.setCaptchaType("blockPuzzle");
        return success(captchaService.get(vo));
    }
    @GetMapping("/verifyCaptcha")
    @ApiOperation("是否需要验证码")
    public CommonResult verifyCaptcha() {
        ConfSystem confSystem = confService.getConfValueByConfKey(ConfKeyEnum.SYSTEM_SAFE.getConfKey());
        return success(confSystem.getOpenVerificationCode());
    }

    @PostMapping("/check")
    @ApiOperation("验证图片验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pointJson", value = "point", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "token", value = "token", dataTypeClass = String.class)
    })
    public CommonResult<ResponseModel> check(@RequestParam("pointJson") String pointJson,@RequestParam("token") String token) {
        CaptchaVO vo = new CaptchaVO();
        vo.setPointJson(pointJson);
        vo.setToken(token);
//        vo.setCaptchaType("blockPuzzle");
        ResponseModel responseModel = captchaService.check(vo);
        return success(responseModel);
    }
}
